<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;


class AddcaidanController extends Controller
{   
    
    //显示添加页面
    public function addcaidan(){
        $stores = DB::table('stores')->get();
        $cats = DB::table('category')->get();        
                       
        return view('addcaidan',[
            'stores'=>$stores,
            'cats'=>$cats
        ]);
    }
    //执行添加菜单
    public function addcaidanpost(){      
        $caidan=DB::table('meals')->insert(['mname'=>$_POST['pre_name'],'mprice'=>$_POST['pre_price'],'ynum'=>$_POST['pre_ynum']]);

        // return $caidan ? 'OK' : 'fail';
        return $caidan ? redirect('listcaidan') : 'fail';
    }

    //展示列表
    public function listcaidan(){
        //$caidan=DB::table('meals')->pagInate(10);
        $caidan = DB::table('stores')
               ->join('category','stores.sid','=','category.sid')
               ->join('meals','category.lid','=','meals.parent_id')
               ->select('stores.*','category.*','meals.*')
               ->orderBy('mid','asc')
               ->paginate(10);
        //dd($caidan);
        return view('listcaidan',['caidan'=>$caidan]);
    }
    //修改菜单  展示修改内容
    public function editcaidan($mid){
            $caidan = DB::table('meals')->where('mid',$mid)->first();
            $store = DB::table('stores')->where('sid',$caidan->sid)->first();
            $cat = DB::table('category')->where('lid',$caidan->parent_id)->first();
            //dd($cat);
            return view('editcaidan',[
                'caidan'=>$caidan,
                'store'=>$store,
                'cat'=>$cat
            ]);
    }
    //执行修改
    public function editcaidanpost($mid){
        //dd($_POST);
            $caidan = DB::table('meals')->where('mid',$mid)->update([
                'mname'=>$_POST['mname'],
                'mprice'=>$_POST['mprice'],
            ]);
        return $caidan ? redirect('listcaidan') : 'fail';
    }
     //删除菜单 
    public function delcaidan($mid){
        if(DB::table('meals')->where('mid',$mid)->delete()){
            return redirect('listcaidan');
        }else{
            return '删除失败';
        }
    }
}
